home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Making Prefs / Making Prefs.c < prev   
Encoding:
C/C++ Source or Header  |  1994-10-25  |  5.6 KB  |  139 lines  |  [TEXT/MMCC]

  1. /****************************************************************************************
  2. Did you ever wonder how to make prefrence files or just how to save to resources in 
  3. general? Me too. Well this file in 'C' contains two functions one that creates a 
  4. preference file in the the preference folder and saves prefs to it. The other reads the 
  5. prefs back. I have tried to document to code extensivly to make it as clear as possible 
  6. and easy to learn from. If you have an questions e-mail me on America Online at Bernard256
  7. Also be sure to check out the Macintosh Development Forum on America Online the guys there
  8. answer all questions. Feel free to distrubute this to all your friends or post it on BBS's,
  9. ftp's etc... By the way you would probably have best luck reading this with Think C or Codewarrior
  10. because I used hard returns. The code here only works in System 7 but if you know 
  11. something about files and resources you could make it Sys 6 compatible. For more on
  12. Files see Inside Macintosh: Files or for resources Inside Macintosh: More Mac ToolBox
  13. ****************************************************************************************/
  14.  
  15. #include "stdio.h"
  16. #include "Folders.h"
  17. #include "string.h"
  18.  
  19. FSSpec mySpec;
  20. long myDirID;
  21. short myResRef, gARef;
  22. short myVRef;
  23.  
  24. void ReadPref(void);
  25. void SavePref(void);
  26.  
  27. struct prefs /*this is the setting struct as well as a handle to it because you save 
  28.                resources using Handles. You'll see*/
  29. {
  30.     Boolean havingFun;
  31.     Boolean registered;
  32.     Boolean soundOn;
  33.     char *Names[5][256];/*high score names array*/
  34.     int Scores[5];/*high score numbers array*/
  35. }settings,**setHan;
  36.  
  37. void main(void)
  38. {
  39.     //Call these functions in the beginning of your application
  40.     gARef = CurResFile();/*keep a permanant refrence to you apps resource fork so we can 
  41.                        always go back to it after we are done fooling around with the 
  42.                        pref files resource fork*/
  43.     setHan = (struct prefs **)NewHandle(sizeof(settings));/*initalize the Handle*/
  44.     SavePref();
  45.     ReadPref();
  46. }
  47. void ReadPref(void)
  48. {
  49.     int err,i;
  50.     OSErr myErr;
  51.     
  52.     myErr = FindFolder(0x8000, 'pref', FALSE, &myVRef, &myDirID);/*Make sure you are in 
  53.                                                                    the preference folder
  54.                                                                    of the startup volume*/
  55.     if(myErr == noErr)/*your cool*/
  56.         myErr = FSMakeFSSpec(myVRef, myDirID, "\pMyPrefs",&mySpec);
  57.     if(myErr==fnfErr)/*you've still created the FSSpec but you don't have a file to attach
  58.                        it to so you can't read the prefs so we must call SavePrefs() 
  59.                        because besides saving prefs SavePrefs() creates the file*/
  60.     {
  61.         SavePref();
  62.     }
  63.     else /*otherwise you have the pref file and can just read from it*/
  64.     {
  65.         myResRef = FSpOpenResFile(&mySpec,3);/*open the file's resource fork*/
  66.         setHan = (struct prefs **)GetResource('????',128);/*'????' stands for the kind of
  67.                                                             resource you are saving to. 
  68.                                                             Just like you normally use 
  69.                                                             'WIND' or 'MENU' resources now
  70.                                                             you are going to use a 
  71.                                                             resource type that you have 
  72.                                                             created in the SavePref()
  73.                                                             function. Anyway we are making
  74.                                                             the Handle setHan equal to the
  75.                                                             Handle of resource id 128. We
  76.                                                             coerce it to fit the same struct
  77.                                                             as setHan*/
  78.         settings.havingFun=(**setHan).havingFun;/*now we set our settings equal to the saved ones*/
  79.         settings.registered=(**setHan).registered;
  80.         settings.soundOn=(**setHan).soundOn;
  81.         for(i=0;i<5;i++)
  82.         {
  83.             settings.Scores[i] =(**setHan).Scores[i];
  84.             strcpy(*settings.Names[i],*(**setHan).Names[i]);
  85.         }
  86.         UseResFile(gARef);/*set the curren resource fork back to your programs*/
  87.     }
  88. }
  89. void SavePref(void)
  90. {
  91.     int err,i;
  92.     OSErr myErr;
  93.     
  94.     myErr = FindFolder(0x8000, 'pref', FALSE, &myVRef, &myDirID);/*Make sure you are in 
  95.                                                                    the preference folder
  96.                                                                    of the startup volume*/
  97.     if(myErr == noErr)
  98.         myErr = FSMakeFSSpec(myVRef, myDirID, "\pMy Prefs",&mySpec);
  99.     if(myErr==fnfErr)/*the file doesn't exist although the FSSpec has been created so now
  100.                        we have to make the file if the file does exist we can just skip
  101.                        ovet the next section and go directly to the writing*/
  102.     {
  103.         FSpCreateResFile(&mySpec, '????','pref',-1);/*create a file of type 'pref' with 
  104.                                                       the creator being whatever 4 letter
  105.                                                       code you put in place of ????*/
  106.         myResRef = FSpOpenResFile(&mySpec,3);        /*Get a refrence to this file's 
  107.                                                       resource fork*/
  108.     }
  109.     UseResFile(myResRef);/*and use this file's resource fork as the current fork*/
  110.     (**setHan).havingFun=settings.havingFun;/*Now save your application's setting to the
  111.                                               resource handle*/
  112.     (**setHan).registered=settings.registered;
  113.     (**setHan).soundOn=settings.soundOn;
  114.     for(i=0;i<5;i++)
  115.         {
  116.             (**setHan).Scores[i] =settings.Scores[i];
  117.             strcpy(*(**setHan).Names[i],*settings.Names[i]);
  118.         }
  119.     if(myErr==fnfErr)/*if we have just created the pref file we have to use the add 
  120.                         resource command to add a resource to the resource fork*/
  121.     {
  122.         AddResource((Handle)setHan,'????',128, "\pPrefs");/*???? is the type (like we
  123.                                                             talked about before) and 128
  124.                                                             is the id and Prefs the name
  125.                                                             The first paramter is a handle
  126.                                                             the the struct cause add 
  127.                                                             resource creates a resource 
  128.                                                             out of a handle (thats why
  129.                                                             we made a handle)*/
  130.     }
  131.     else /*if we are calling it just to update the file we have already made the just
  132.            call Changed resource and it knows which ones*/
  133.     {
  134.         ChangedResource((Handle)setHan);
  135.           UpdateResFile(myResRef);/*update the file's resource fork*/
  136.       }
  137.  
  138.     UseResFile(gARef);/*set the current resource fork back to your applications*/
  139. }